class PriceList < ActiveRecord::Base
  has_many                  :photo_sizes,   :order => "cost asc"
  has_many                  :galleries
  has_many                  :item_options
  validates_presence_of     :name
  
  before_destroy            :reassign_orphanined_galleries_and_photo_sizes
  after_destroy             :ensure_atleast_one_price_list_persists
  
protected
  def reassign_orphanined_galleries_and_photo_sizes
    self.galleries.each {|gallery| gallery.update_attributes!(:price_list => nil)}
    self.photo_sizes.each {|photo_size| photo_size.update_attributes!(:price_list => nil)}
  end
  def ensure_atleast_one_price_list_persists
    PriceList.create!(:name => "default") if PriceList.find(:all).empty?
  end
end
